﻿//======================================================================
//  邮件,短息发送器
//====================================================================== 

using System;
using System.Collections.Generic;

using System.Text;
using System.Net.Mail;

namespace Common
{
    /// <summary>
    /// 邮件,短息发送器
    /// </summary>
    public sealed class Sender
    {
        #region 发送邮件

        /// <summary>
        /// 发送邮件(使用.net自带组件发送)
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public static bool sendMail(Email mail)
        {
            bool success = false;
            MailMessage msg = new MailMessage();
            msg.To.Add(mail.To);

            msg.From = new MailAddress(mail.From, mail.From, mail.CharSet);
            msg.Subject = mail.Subject;
            msg.SubjectEncoding = mail.CharSet;
            msg.Body = mail.Body;
            msg.BodyEncoding = mail.CharSet;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential(mail.Login, mail.Password);
            client.Port = mail.Port;
            client.Host = mail.SMTPServer;

            try
            {
                client.Send(msg);

                success = true;
            }
            catch (Exception e)
            {
                string errMsg = e.Message;
            }

            return success;
        }

        /// <summary>
        /// 发送电子邮件(使用.net自带组件发送)
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">邮件内容</param>
        public static bool sendMail(string smtpserver, string userName, string pwd, string strfrom, string strto, string subj, string bodys)
        {
            bool success = false;
            try
            {
                SmtpClient _smtpClient = new SmtpClient();
                _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                _smtpClient.Host = smtpserver; ;//指定SMTP服务器
                _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码

                MailMessage _mailMessage = new MailMessage(strfrom, strto);
                _mailMessage.Subject = subj;//主题
                _mailMessage.Body = bodys;//内容
                _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
                _mailMessage.IsBodyHtml = true;//设置为HTML格式
                _mailMessage.Priority = MailPriority.High;//优先级
                _smtpClient.Send(_mailMessage);
                success = true;
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
                success = false;
            }

            return success;
        }

        #endregion
    }
}
